package bean;

import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;

import javax.jdo.Query;

import pmf.PMF;

public class SearchArt {

	@SuppressWarnings("unchecked")
	public List<Articles> searchByItem(String searchName) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Articles.class);

		query.setFilter("artName == searchName");
		query.setOrdering("buyDate desc");
		query.declareParameters("String searchName");

		try {
			List<Articles> results = (List<Articles>) query.execute(searchName);
			List<Articles> artList = new LinkedList<Articles>();

			for (Articles tempArt : results) {
				artList.add(new Articles(tempArt.getKey(), tempArt.getCreateUserID(),tempArt.getCategory(), 
						tempArt.getArtName(), tempArt.getPrice(), tempArt.getShipWay(), tempArt.getBuyDate(), 
						tempArt.getBranch(), tempArt.getTotalAmount(), tempArt.getRemainAmount(), 
						tempArt.getParticipate(), tempArt.getShipFee(), tempArt.getNote()));
			}
			return artList;
		} finally {
			query.closeAll();
			pm.close();
		}
	}
	
	public Articles searchById(long id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			
			Articles art = pm.getObjectById(Articles.class, id);
			Articles saveArt = new Articles(art.getKey(), art.getCreateUserID(), art.getCategory(),
				art.getArtName(), art.getPrice(), art.getShipWay(), art.getBuyDate(), art.getBranch(), 
				art.getTotalAmount(), art.getRemainAmount(), art.getParticipate(), art.getShipFee(), 
				art.getNote());
			
			return saveArt;
		}finally{
			pm.close();
		}		
	}

}
